package dd.lo.ml;

import dd.lo.HelloCV;
import dd.lo.ml.enums.ImgLabel;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.MatOfFloat;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.ml.SVM;

public class ImgClassificationPredictor {

    private static final String ROOT = HelloCV.class.getResource("/").getPath();

    static {
        System.load(ROOT + "libopencv_java481.dylib");
    }

    public static void main(String[] args) {
        //读取SVM模型
        SVM predictor = SVM.load(ROOT + "svm_classification_v_1_0.xml");
        System.out.printf("Model coef: %f\n", predictor.getCoef0());
        //使用训练好的模型进行预测（使用train数据）
//        testImage(predictor, "/Users/kwok/Downloads/img_classification_train_data/western/12332_45_img_0.jpeg");
//        testImage(predictor, "/Users/kwok/Downloads/img_classification_train_data/scater/pcbi_3_img_0.jpeg");
//        testImage(predictor, "/Users/kwok/Downloads/img_classification_train_data/chip/pone_14_img_6.jpeg");
//        testImage(predictor, "/Users/kwok/Downloads/img_classification_train_data/chart/Circular RNA circ-ABCB10 promotes non-small cell lung cancer proliferation and inhibits cell apoptosis through repressing KISS1_3_img_4.jpeg");
//        testImage(predictor, "/Users/kwok/Downloads/img_classification_train_data/bright_field/LncRNA INHBA-AS1 promotes cell growth, migration, and invasion of oral squamous cell carcinoma by sponging miR-143-3p_2_img_2.jpeg");
//        testImage(predictor, "/Users/kwok/Downloads/img_classification_train_data/tissue/gar_pone_3_img_2.jpeg");
//        testImage(predictor, "/Users/kwok/Downloads/img_classification_train_data/text/Mon Jan 29 15:46:50 CST 2024_21_img_18.jpeg");
        //使用训练好的模型进行预测（使用test数据）
        testImage(predictor, "/Users/kwok/Downloads/img_classification_test_data/western/Long noncoding RNA LINC00052 suppressed the proliferation, migration and invasion of glioma cells by upregulating KLF6_4_img_4.jpeg");
        testImage(predictor, "/Users/kwok/Downloads/img_classification_test_data/scater/Mon Jan 29 15:46:50 CST 2024_20_img_5.jpeg");
        testImage(predictor, "/Users/kwok/Downloads/img_classification_test_data/fluorescent/test_pdf_2_img_0.jpeg");
        testImage(predictor, "/Users/kwok/Downloads/img_classification_test_data/fluorescent/LGR5 Promotes Breast Cancer Progression and Maintains Stem8208Like Cells Through Activation of Wnt9468208Catenin Signaling_5_img_64.jpeg");
        testImage(predictor, "/Users/kwok/Downloads/img_classification_test_data/chip/12332_19_img_0.jpeg");
        testImage(predictor, "/Users/kwok/Downloads/img_classification_test_data/chart/test_pdf_32_img_0.jpeg");
        testImage(predictor, "/Users/kwok/Downloads/img_classification_test_data/photo/LGR5 Promotes Breast Cancer Progression and Maintains Stem8208Like Cells Through Activation of Wnt9468208Catenin Signaling_5_img_45.jpeg");
        testImage(predictor, "/Users/kwok/Downloads/img_classification_test_data/bright_field/gar_pone_2_img_1.jpeg");
        testImage(predictor, "/Users/kwok/Downloads/img_classification_test_data/bright_field/Long non‐coding RNA TTN‐AS1 promotes breast cancer cell migration and invasion via sponging miR‐140‐5p_2_img_9.jpeg");
        testImage(predictor, "/Users/kwok/Downloads/img_classification_test_data/bright_field/Circular RNA circ-ABCB10 promotes non-small cell lung cancer proliferation and inhibits cell apoptosis through repressing KISS1_2_img_3.jpeg");
        testImage(predictor, "/Users/kwok/Downloads/img_classification_test_data/tissue/12332_72_img_0.jpeg");
        testImage(predictor, "/Users/kwok/Downloads/img_classification_test_data/text/Mon Jan 29 15:46:50 CST 2024_17_img_16.jpeg");
    }

    private static void testImage(SVM predictor, String imgPath) {
        Mat testImg = Imgcodecs.imread(imgPath, Imgcodecs.IMREAD_GRAYSCALE);
        MatOfFloat testFeature = FeatureExtraction.computeGrayImgHogFeature(testImg);
        Mat testSample = new Mat(1, testFeature.height(), CvType.CV_32FC1); // 创建一个测试样本
        testSample.put(0, 0, testFeature.toArray()); // 设置测试样本特征值
        double result = predictor.predict(testSample); // 进行预测
        ImgLabel predictedLabel = ImgLabel.getByLabel((int) result);
        System.out.println("Predicted label: " + predictedLabel.getName()); // 输出预测结果
    }
}
